package com.gz.lot.user.controller;

import com.gz.lot.user.User;
import com.gz.lot.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 董诗攀
 */
@RestController
//Api注解，描述信息 可通过tag进行分类
@Api(value = "UserController")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 注册
     * @param user
     * @return
     */
    @PostMapping("register")
    //方法描述
    @ApiOperation(notes = "注册", value = "register")
    public ResponseEntity<Void> register(User user) {
        Boolean boo = this.userService.register(user);
        if (boo == null || !boo) {
            return ResponseEntity.badRequest().build();
        }
        return  ResponseEntity.status(HttpStatus.CREATED).build();
    }
    /**
     * 查询用户列表
     * @return
     */
    @GetMapping("users")
    //方法描述
    @ApiOperation(notes = "查询列表", value = "users")
    public ResponseEntity<List<User>> queryUserList() {
        List<User> users = this.userService.queryUserList();
        if(users == null){
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return ResponseEntity.ok(users);
    }

    @PostMapping("login")
    //方法描述
    @ApiOperation(notes = "登录", value = "login")
    public ResponseEntity<Void> login(User user) {
        System.out.println(user);
        if (user.getUsername() != null) {
            Boolean boo = this.userService.login(user);
            if (boo == null || !boo) {
                return ResponseEntity.badRequest().build();
            }
        }
        return  ResponseEntity.status(HttpStatus.CREATED).build();
    }

}
